//
//  UIButton+Extension.m
//  爱车生活
//
//  Created by Yiwen23 on 2017/6/30.
//  Copyright © 2017年 Fw. All rights reserved.
//

#import "UIButton+Extension.h"
#import <objc/runtime.h>

@implementation UIButton (Extension)
/*
 图左文右
 **/
//+(UIButton *)createBtnWithImgLeftAndTitleRightFrame:(CGRect)frame norTitle:(NSString *)norTitle hTitle:(NSString *)hTitle font:(UIFont *)font norImage:(UIImage *)norImage hImage:(UIImage *)hImage imageEdge:(UIEdgeInsets)imageEdge titleEdge:(UIEdgeInsets)titleEdge target:(id)target action:(SEL)action
//{
//    UIButton * btn= [[UIButton alloc]initWithFrame:frame];
//    [btn setTitle:NSLocalizedString(norTitle, nil) forState:UIControlStateNormal];
//    [btn setTitle:NSLocalizedString(hTitle, nil) forState:UIControlStateSelected];
//    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    btn.titleLabel.font = font;
//    [btn setImage:norImage forState:UIControlStateNormal];
//    [btn setImage:norImage forState:UIControlStateHighlighted];
//    [btn setImage:hImage forState:UIControlStateSelected];
//    [btn setImageEdgeInsets:imageEdge];
//    [btn setTitleEdgeInsets:titleEdge];
//    if (target && action) {
//        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
//    }
//    return btn;
//}
/*
 文字
 **/
+(UIButton *)createOnlyTitleBtnFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)textColor titleFont:(UIFont *)textfont target:(id)target action:(SEL)action

{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:NSLocalizedString(title, nil) forState:UIControlStateNormal];
    [btn setTitleColor:textColor forState:UIControlStateNormal];
    btn.titleLabel.font = textfont;
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return btn;
}
/*
  文字 + 背景图
 **/
+(UIButton *)createBtnWithTitleAndBgImgFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)textColor titleFont:(UIFont *)textfont bgImageName:(NSString *)bgImageName target:(id)target action:(SEL)action

{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:NSLocalizedString(title, nil) forState:UIControlStateNormal];
    [btn setTitleColor:textColor forState:UIControlStateNormal];
    btn.titleLabel.font = textfont;
    [btn setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return btn;
}

/*
  纯图片
 **/
+(UIButton *)createBtnWithOnlyImageFrame:(CGRect)frame norBGImage:(UIImage *)norImage seleBGimage:(UIImage *)selImage target:(id)target action:(SEL)action

{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setBackgroundImage:norImage forState:UIControlStateNormal];
    [btn setBackgroundImage:selImage forState:UIControlStateSelected];
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return btn;
    
}

//!图右文左
+(UIButton *)createBtnWithTitleLeftAndImgRightFrame:(CGRect)frame kMargin:(CGFloat)margin title:(NSString *)title titleFont:(CGFloat)font titleColor:(UIColor *)titleColor Image:(NSString *)Image ImageWidth:(CGFloat)imageW target:(id)target action:(SEL)action{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    CGSize size = [title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil]];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width, btn.height)];
    label.text = title;
    label.backgroundColor = [UIColor whiteColor];
    label.textColor = titleColor;
    label.font = [UIFont systemFontOfSize:font];
    [btn addSubview:label];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:Image]];
    imageView.frame = CGRectMake(CGRectGetMaxX(label.frame) + margin, (btn.height - imageW)/2, imageW, imageW);
    [btn addSubview:imageView];
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return btn;
}
//!图左文右
+(UIButton *)createBtnWithImgLeftAndTitleRightFrame:(CGRect)frame kMargin:(CGFloat)margin title:(NSString *)title titleFont:(CGFloat)font titleColor:(UIColor *)titleColor Image:(NSString *)Image ImageWidth:(CGFloat)imageW target:(id)target action:(SEL)action{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:Image]];
    imageView.frame = CGRectMake(3, (btn.height - imageW)/2, imageW, imageW);
    [btn addSubview:imageView];
    
    CGSize size = [title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil]];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + margin, 0, size.width, btn.height)];
    label.text = title;
    label.textColor = titleColor;
    label.backgroundColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:font];
    [btn addSubview:label];

    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return btn;
}
//!图上文下
+(UIButton *)createBtnWithImgUpAndTitleDownFrame:(CGRect)frame kMargin:(CGFloat)margin title:(NSString *)title titleFont:(CGFloat)font titleColor:(UIColor *)titleColor Image:(NSString *)Image ImageWidth:(CGFloat)imageW target:(id)target action:(SEL)action{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:Image]];
    imageView.frame = CGRectMake((btn.width - imageW)/2, 0, imageW, imageW);
    imageView.layer.cornerRadius = imageW/2;
    imageView.clipsToBounds = YES;
    [btn addSubview:imageView];
    
//    CGSize size = [title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil]];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame), btn.width, btn.height - imageW)];
    label.text = title;
    label.textColor = titleColor;
    label.backgroundColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = NSTextAlignmentCenter;
    [btn addSubview:label];
    
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return btn;
}

/*
 防止button重复点击，设置间隔
 */
static const char *UIButton_acceptEventInterval = "UIButton_acceptEventInterval";
static const char *UIButton_acceptEventTime     = "UIButton_acceptEventTime";

- (NSTimeInterval )mm_acceptEventInterval{
    return [objc_getAssociatedObject(self, UIButton_acceptEventInterval) doubleValue];
}

- (void)setMm_acceptEventInterval:(NSTimeInterval)mm_acceptEventInterval{
    objc_setAssociatedObject(self, UIButton_acceptEventInterval, @(mm_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval )mm_acceptEventTime{
    return [objc_getAssociatedObject(self, UIButton_acceptEventTime) doubleValue];
}

- (void)setMm_acceptEventTime:(NSTimeInterval)mm_acceptEventTime{
    objc_setAssociatedObject(self, UIButton_acceptEventTime, @(mm_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+(void)load {
    //获取这两个方法
    Method systemMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    SEL sysSEL = @selector(sendAction:to:forEvent:);
    
    Method myMethod = class_getInstanceMethod(self, @selector(mm_sendAction:to:forEvent:));
    SEL mySEL = @selector(mm_sendAction:to:forEvent:);
 
    //添加方法进去
    BOOL didAddMethod = class_addMethod(self, sysSEL, method_getImplementation(myMethod), method_getTypeEncoding(myMethod));
    
    //如果方法已经存在
    if (didAddMethod) {
        class_replaceMethod(self, mySEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
    }else{
        method_exchangeImplementations(systemMethod, myMethod);
        
    }
    
    /*-----以上主要是实现两个方法的互换,load是gcd的只shareinstance，保证执行一次-------*/
}
- (void)mm_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    
    if (NSDate.date.timeIntervalSince1970 - self.mm_acceptEventTime < self.mm_acceptEventInterval) {
        return;
    }
    
    if (self.mm_acceptEventInterval > 0) {
        self.mm_acceptEventTime = NSDate.date.timeIntervalSince1970;
    }
    
    [self mm_sendAction:action to:target forEvent:event];
}

@end
